package com.example.studentregistration.service.impl;

import com.example.studentregistration.mapper.StudentAppMapper;
import com.example.studentregistration.pojo.StudentApp;
import com.example.studentregistration.service.StudentAppService;
import com.example.studentregistration.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentAppServiceImpl implements StudentAppService {

    @Autowired
    private StudentAppMapper studentAppMapper;

    /* 提交：只允许修改联系方式、地址、紧急联系人三栏，其余只读 */
    @Override
    @Transactional
    public Result<?> submitStudentInfo(StudentApp dto) {
        int rows = studentAppMapper.updateStudentInfo(dto);
        if (rows == 0) return Result.fail("学生信息不存在");
        return Result.success("提交成功");
    }

    /* 回显：只返回基本信息，密码等敏感字段已脱敏 */
    @Override
    public Result<StudentApp> getStudentInfoByNo(String studentNo) {
        StudentApp student = studentAppMapper.selectByStudentNo(studentNo);
        if (student == null) return Result.fail("学生不存在");
        // 脱敏：不把密码完整返回
        student.setPassword(null);
        return Result.success(student);
    }
    @Override
    public Result<StudentApp> login(String phone, String password) {
        StudentApp student = studentAppMapper.login(phone, password);
        return student == null ? Result.fail("账号或密码错误") : Result.success(student);
    }


}
